package com.hxf.web.action.user;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.GlobalMPURL;
import com.hxf.common.PublicUtil;
import com.hxf.dto.AdminRoleDto;
import com.hxf.dto.AdminUserDto;
import com.hxf.dto.AdminUserRoleDto;
import com.hxf.dto.AppUserDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;
import com.hxf.web.vo.UserRoleVo;

@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class AdminUserAction extends WebActionBase {
	
	private static final long serialVersionUID = 1L;
	
	public List<AdminUserDto> adminUserList;
	public List<UserRoleVo> selectRoleList;
	public AdminUserDto adminUserDto;
	private Integer id;
	private String userName;
	private String userPwd;
	private Integer userSex;
	private String realName;
	private String userIdNo;
	private String userPhone;
	private String userAddress;
	private String userZipCode;
	private String qq;
	private String userEmail;
	private Integer status;  //状态
	private String userIds;
	private String roleIds;
	private Integer userId;//角色
	
	public String userList(){

		if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
			return "msg";
		} 
		//AdminUserDto ud = new AdminUserDto();
		
		//UserDto dto = new UserDto();
		//dto = getUser();
		//ud.setId(dto.getUserId());//传当前登录用户 ID 过去
		
		try{
			//会员信息
			ReturnData<AdminUserDto> userlist = super.serviceFacade.getAdminUserService().findAdminUser(null, targetPage, pageSize);
			adminUserList = userlist.getResultlist();
			this.setRecordCount(userlist.getReturnResult());
		}catch(ServiceException e){
			e.printStackTrace();
		}
		return "admin-user-list";
	}

	/**
	 * 添加 后台管理人员
	 * @return
	 */
	public String adminUserAdd(){
		return "admin-user-add";
	}
	
	/**
	 * 添加或者修改
	 * @return
	 */
	public String userSaveOrUpdate() {
		try {
			//菜单权限验证 
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			}
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			
			AdminUserDto ud = new AdminUserDto();
			ud.setUserName(userName);//用户名
			ud.setRealName(realName);//真实姓名
			ud.setUserSex(userSex);//性别
			ud.setUserIdNo(userIdNo);//身份证号码
			ud.setUserPhone(userPhone);//电话
			ud.setUserEmail(userEmail);//邮箱
			ud.setUserPwd(userPwd);//密码
			
			//管理员登陆
			if(null != adminUserDto){
				ud.setParentId(getAdminUser().getId());//上级ID
				ud = (AdminUserDto) super.serviceFacade.getAdminUserService().get(AdminUserDto.class, Integer.valueOf(getAdminUser().getId()));
				//保存上级ID
				if(PublicUtil.isNotNullAndEmpty(ud.getParentUserId())){
					ud.setParentUserId(ud.getParentUserId()+","+getAdminUser().getId());//上级所有ID,以,分割 
				}else{
					ud.setParentUserId(getAdminUser().getId().toString());
				}
			}else if(null != restaurentUserDto){
				ud.setParentId(getRestaurentUserDto().getId());
			}
			
			if (id != null && !"null".equals(id)) {  

				ud.setId(id);
				super.serviceFacade.getAdminUserService().update(ud);//修改
			} else {
				
				ud.setCreateTime(DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M));//当前时间
				super.serviceFacade.getAdminUserService().save(ud);//保存
			}
			
			requestURL = "admin-user!userList.html";
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}

		return SUCCESS;
	}
	
	/**
	 * 管理员后台添加
	 * @return
	 */
	public String adminUserDelete(){
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			super.serviceFacade.getAdminUserService().delete(adminUserDto);
			super.println("success");
		} catch (ServiceException e) { 
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		} 
		return null;
	}
	
	/**
	 * 修改用户信息
	 * @return
	 */
	public String userUpdate() { 
		try {
			//菜单权限验证
			if(!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)){
				return "msg";
			}
			
			adminUserDto = (AdminUserDto) super.serviceFacade.getAdminUserService().get(AdminUserDto.class, id);
		} catch (ServiceException e){
			logger.debug(e.getMessage(), e);
		}
		return "admin-user-update";
	}
	
	/**
	 * 修改会员状态
	 * @return
	 */
	public String updateUserStatus() {
		//菜单权限验证
		if (!menuPrivilegeValidate(GlobalMPURL.APP_USER_URL)) {
			return "msg";
		}  
		try {
			if ("".equals(userIds) || null == userIds || status == null) {
				println(-1);
				return null;
			}
			super.serviceFacade.getAdminUserService().updateUserStatus(userIds, status);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	// 联盟管理员 绑定角色
	public String userAddRole() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			//adminUserDto = (AdminUserDto)super.serviceFacade.getUserService().get(UserDto.class, adminUserDto.getId());
			AdminUserDto urDto = new AdminUserDto();
			urDto.setId(id);//点击角色绑定 id
			getSession().setAttribute("roleId", id);//获取绑定角色ID
			//AdminUserDto userDto = (AdminUserDto)getSession().getAttribute("adminUserDto");//当前登陆用户
			//AdminUserDto user = getUser();
			//AdminUserRoleDto adminUserRoleDto = new AdminUserRoleDto();
			//adminUserRoleDto.setUserId(userId);
			
			selectRoleList = super.serviceFacade.getAdminUserService().findByUserAddRole(urDto,id);
			
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "admin-user-role";
	}
	
	/**
	 * 保存添加角色
	 * @return
	 */
	public String userAddRoleSave() { 
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			}
			//AdminUserDto adminUserDto = (AdminUserDto) getSession().getAttribute("adminUserDto");
			AdminUserRoleDto adminUserRoleDto = new AdminUserRoleDto();
			
			adminUserRoleDto.setUserId(null==getSession().getAttribute("roleId")?null:Integer.valueOf(getSession().getAttribute("roleId").toString()));
			super.serviceFacade.getAdminUserService().saveUserAddRole(adminUserRoleDto, roleIds);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		requestURL = "admin-user!userList.html";
		return SUCCESS;
	}
	
	public List<AdminUserDto> getAdminUserList() {
		return adminUserList;
	}

	public void setAdminUserList(List<AdminUserDto> adminUserList) {
		this.adminUserList = adminUserList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public Integer getUserSex() {
		return userSex;
	}

	public void setUserSex(Integer userSex) {
		this.userSex = userSex;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getUserIdNo() {
		return userIdNo;
	}

	public void setUserIdNo(String userIdNo) {
		this.userIdNo = userIdNo;
	}

	public String getUserPhone() {
		return userPhone;
	}

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

	public String getUserAddress() {
		return userAddress;
	}

	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}

	public String getUserZipCode() {
		return userZipCode;
	}

	public void setUserZipCode(String userZipCode) {
		this.userZipCode = userZipCode;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}

	public AdminUserDto getAdminUserDto() {
		return adminUserDto;
	}

	public void setAdminUserDto(AdminUserDto adminUserDto) {
		this.adminUserDto = adminUserDto;
	}

	public List<UserRoleVo> getSelectRoleList() {
		return selectRoleList;
	}

	public void setSelectRoleList(List<UserRoleVo> selectRoleList) {
		this.selectRoleList = selectRoleList;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	
}
